import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useCounterStore } from "@/stores/counter";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'getUser',
      component: () => import('../views/GetUser.vue'),
    },
    {
      path: '/HomeView',
      name: 'HomeView',
      component: () => import('../views/HomeView.vue'),
      redirect: '/ListTrace',
      children:[
        {
          path: '/ListTrace',
          name: 'ListTrace',
          component: () => import('../views/ListTrace.vue'),
          alias: ['/listtrace']
        },
        {
          path: '/Cultivation',
          name: 'Cultivation',
          component: () => import('../views/Cultivation.vue'),
        },
      ]
    },
  ],
})

router.beforeEach((to, from, next) => {
  var pinia= useCounterStore()
  if (to.name !== 'getUser' && pinia.login.token=="")
    {
     next({ name: 'getUser' })
    } 

  // 如果用户未能验证身份，则 `next` 会被调用两次
  next()
})


export default router



